﻿Public Class ucApplication2
    Inherits System.Web.UI.UserControl

	Public Event NextStep()
	Public Event BackStep()
	Public Event Cancel()


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
		If Not Page.IsPostBack Then
			LoadProvince1()
			LoadProvince2()

			LoadDistinct1()
			LoadDistinct2()

			LoadSubDistinct1()
			LoadSubDistinct2()

			LoadPostCode1()
			LoadPostCode2()

			LoadHeader()

			If GlobalApp.RegisterInfo IsNot Nothing Then
				LoadData()
			End If
		End If
    End Sub

	Private Sub LoadProvince1()
		OFFICER_CARD_ADDRESS_PROVINCE.DataTextField = "PROVINCE_NAME"
		OFFICER_CARD_ADDRESS_PROVINCE.DataValueField = "PROVINCE_ID"
		OFFICER_CARD_ADDRESS_PROVINCE.DataSource = BL.Province.GetAll
		OFFICER_CARD_ADDRESS_PROVINCE.DataBind()
		OFFICER_CARD_ADDRESS_PROVINCE.Items.Insert(0, New ListItem("--กรุณาระบุจังหวัด--", "0"))
	End Sub

	Private Sub LoadProvince2()
		OFFICER_MAIL_ADDRESS_PROVINCE.DataTextField = "PROVINCE_NAME"
		OFFICER_MAIL_ADDRESS_PROVINCE.DataValueField = "PROVINCE_ID"
		OFFICER_MAIL_ADDRESS_PROVINCE.DataSource = BL.Province.GetAll
		OFFICER_MAIL_ADDRESS_PROVINCE.DataBind()
		OFFICER_MAIL_ADDRESS_PROVINCE.Items.Insert(0, New ListItem("--กรุณาระบุจังหวัด--", "0"))
	End Sub

	Private Sub LoadDistinct1()
		If Not Me.OFFICER_CARD_ADDRESS_PROVINCE.SelectedValue.Equals("0") Then
			OFFICER_CARD_ADDRESS_DISTRICT.DataTextField = "DISTRICT_NAME"
			OFFICER_CARD_ADDRESS_DISTRICT.DataValueField = "DISTRICT_ID"
			OFFICER_CARD_ADDRESS_DISTRICT.DataSource = BL.District.GetAll(Me.OFFICER_CARD_ADDRESS_PROVINCE.SelectedValue)
			OFFICER_CARD_ADDRESS_DISTRICT.DataBind()
		Else
			OFFICER_CARD_ADDRESS_DISTRICT.Items.Clear()
		End If
		OFFICER_CARD_ADDRESS_DISTRICT.Items.Insert(0, New ListItem("--กรุณาระบุเขต/อำเภอ--", "0"))
	End Sub

	Private Sub LoadDistinct2()
		If Not Me.OFFICER_MAIL_ADDRESS_PROVINCE.SelectedValue.Equals("0") Then
			OFFICER_MAIL_ADDRESS_DISTRICT.DataTextField = "DISTRICT_NAME"
			OFFICER_MAIL_ADDRESS_DISTRICT.DataValueField = "DISTRICT_ID"
			OFFICER_MAIL_ADDRESS_DISTRICT.DataSource = BL.District.GetAll(Me.OFFICER_MAIL_ADDRESS_PROVINCE.SelectedValue)
			OFFICER_MAIL_ADDRESS_DISTRICT.DataBind()
		Else
			OFFICER_MAIL_ADDRESS_DISTRICT.Items.Clear()
		End If
		OFFICER_MAIL_ADDRESS_DISTRICT.Items.Insert(0, New ListItem("--กรุณาระบุเขต/อำเภอ--", "0"))
	End Sub

	Private Sub LoadSubDistinct1()
		If Not Me.OFFICER_CARD_ADDRESS_DISTRICT.SelectedValue.Equals("0") Then
			officer_card_address_subICT.DataTextField = "SUB_DISTRICT_NAME"
			officer_card_address_subICT.DataValueField = "SUB_DISTRICT_ID"
			officer_card_address_subICT.DataSource = BL.SubDistrict.AllSubDistrict(Me.OFFICER_CARD_ADDRESS_PROVINCE.SelectedValue, Me.OFFICER_CARD_ADDRESS_DISTRICT.SelectedValue)
			officer_card_address_subICT.DataBind()
		Else
			officer_card_address_subICT.Items.Clear()
		End If
		officer_card_address_subICT.Items.Insert(0, New ListItem("--กรุณาระบุแขวง/ตำบล--", "0"))
	End Sub

	Private Sub LoadSubDistinct2()
		If Not Me.OFFICER_MAIL_ADDRESS_DISTRICT.SelectedValue.Equals("0") Then
			OFFICER_MAIL_ADDRESS_SUB_DISTRICT.DataTextField = "SUB_DISTRICT_NAME"
			OFFICER_MAIL_ADDRESS_SUB_DISTRICT.DataValueField = "SUB_DISTRICT_ID"
			OFFICER_MAIL_ADDRESS_SUB_DISTRICT.DataSource = BL.SubDistrict.AllSubDistrict(Me.OFFICER_MAIL_ADDRESS_PROVINCE.SelectedValue, Me.OFFICER_MAIL_ADDRESS_DISTRICT.SelectedValue)
			OFFICER_MAIL_ADDRESS_SUB_DISTRICT.DataBind()
		Else
			OFFICER_MAIL_ADDRESS_SUB_DISTRICT.Items.Clear()
		End If
		OFFICER_MAIL_ADDRESS_SUB_DISTRICT.Items.Insert(0, New ListItem("--กรุณาระบุแขวง/ตำบล--", "0"))
	End Sub

	Private Sub LoadPostCode1()
		If Not Me.officer_card_address_subICT.SelectedValue.Equals("0") Then
			OFFICER_CARD_ADDRESS_POSTCODE.DataTextField = "POSTCODE"
			OFFICER_CARD_ADDRESS_POSTCODE.DataValueField = "POSTCODE"
			OFFICER_CARD_ADDRESS_POSTCODE.DataSource = BL.PostCode.AllPostCode(Me.OFFICER_CARD_ADDRESS_PROVINCE.SelectedValue, Me.OFFICER_CARD_ADDRESS_DISTRICT.SelectedValue)
			OFFICER_CARD_ADDRESS_POSTCODE.DataBind()
		Else
			OFFICER_CARD_ADDRESS_POSTCODE.Items.Clear()
		End If
		OFFICER_CARD_ADDRESS_POSTCODE.Items.Insert(0, New ListItem("--รหัสไปรษณีย์--", "0"))
	End Sub

	Private Sub LoadPostCode2()
		If Not Me.OFFICER_MAIL_ADDRESS_SUB_DISTRICT.SelectedValue.Equals("0") Then
			OFFICER_MAIL_ADDRESS_POSTCODE.DataTextField = "POSTCODE"
			OFFICER_MAIL_ADDRESS_POSTCODE.DataValueField = "POSTCODE"
			OFFICER_MAIL_ADDRESS_POSTCODE.DataSource = BL.PostCode.AllPostCode(Me.OFFICER_MAIL_ADDRESS_PROVINCE.SelectedValue, Me.OFFICER_MAIL_ADDRESS_DISTRICT.SelectedValue)
			OFFICER_MAIL_ADDRESS_POSTCODE.DataBind()
		Else
			OFFICER_MAIL_ADDRESS_POSTCODE.Items.Clear()
		End If
		OFFICER_MAIL_ADDRESS_POSTCODE.Items.Insert(0, New ListItem("--รหัสไปรษณีย์--", "0"))
	End Sub

	Public Sub LoadHeader()
		Me.ucHeaderInfo1.LoadData()
	End Sub

	Protected Sub btnBack_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnBack.Click
		Response.Redirect("CreateApplication.aspx", False)
		RaiseEvent BackStep()
	End Sub

	Protected Sub btnNext_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnNext.Click
		Dim registerInfo As Entity.OfficerInfoItem
		If GlobalApp.RegisterInfo IsNot Nothing Then
			registerInfo = GlobalApp.RegisterInfo
		Else
			registerInfo = New Entity.OfficerInfoItem
		End If

		With registerInfo
			.officer_card_address_no = Me.OFFICER_CARD_ADDRESS_NO.Text
			.officer_card_address_moo = Me.OFFICER_CARD_ADDRESS_MOO.Text
			.officer_card_address_building = Me.OFFICER_CARD_ADDRESS_BUILDING.Text
			.officer_card_address_soi = Me.OFFICER_CARD_ADDRESS_SOI.Text
			.officer_card_address_street = Me.OFFICER_CARD_ADDRESS_STREET.Text
			.officer_card_address_province = Me.OFFICER_CARD_ADDRESS_PROVINCE.SelectedItem.Text
			.officer_card_address_district = Me.OFFICER_CARD_ADDRESS_DISTRICT.SelectedItem.Text
			.officer_card_address_sub = Me.officer_card_address_subICT.SelectedItem.Text
			.officer_card_address_postcode = Me.OFFICER_CARD_ADDRESS_POSTCODE.SelectedItem.Text

			.officer_mail_address_no = Me.OFFICER_MAIL_ADDRESS_NO.Text
			.officer_mail_address_moo = Me.OFFICER_MAIL_ADDRESS_MOO.Text
			.officer_mail_address_building = Me.OFFICER_MAIL_ADDRESS_BUILDING.Text
			.officer_mail_address_soi = Me.OFFICER_MAIL_ADDRESS_SOI.Text
			.officer_mail_address_street = Me.OFFICER_MAIL_ADDRESS_STREET.Text
			.officer_mail_address_province = Me.OFFICER_MAIL_ADDRESS_PROVINCE.SelectedItem.Text
			.officer_mail_address_district = Me.OFFICER_MAIL_ADDRESS_DISTRICT.SelectedItem.Text
			.officer_mail_address_sub = Me.OFFICER_MAIL_ADDRESS_SUB_DISTRICT.SelectedItem.Text
			.officer_mail_address_postcode = Me.OFFICER_MAIL_ADDRESS_POSTCODE.SelectedItem.Text
			.officer_mail_phone = Me.OFFICER_MAIL_PHONE.Text
			.officer_mail_email = Me.OFFICER_MAIL_EMAIL.Text
		End With

		GlobalApp.RegisterInfo = registerInfo
		RaiseEvent NextStep()
		Response.Redirect("CreateApplication2.aspx", False)
	End Sub

	Private Sub LoadData()

		Dim registerInfo As Entity.OfficerInfoItem
		registerInfo = GlobalApp.RegisterInfo
		With registerInfo
			Me.OFFICER_CARD_ADDRESS_NO.Text = .officer_card_address_no
			Me.OFFICER_CARD_ADDRESS_MOO.Text = .officer_card_address_moo
			Me.OFFICER_CARD_ADDRESS_BUILDING.Text = .officer_card_address_building
			Me.OFFICER_CARD_ADDRESS_SOI.Text = .officer_card_address_soi
			Me.OFFICER_CARD_ADDRESS_STREET.Text = .officer_card_address_street

			Common.Utility.SelectChoiceByText(OFFICER_CARD_ADDRESS_PROVINCE, .officer_card_address_province)
			Me.LoadDistinct1()
			Common.Utility.SelectChoiceByText(OFFICER_CARD_ADDRESS_DISTRICT, .officer_card_address_district)
			Me.LoadSubDistinct1()
			Common.Utility.SelectChoiceByText(officer_card_address_subICT, .officer_card_address_sub)
			Me.LoadPostCode1()
			Common.Utility.SelectChoiceByText(OFFICER_CARD_ADDRESS_POSTCODE, .officer_card_address_postcode)

			Me.OFFICER_MAIL_ADDRESS_NO.Text = .officer_mail_address_no
			Me.OFFICER_MAIL_ADDRESS_MOO.Text = .officer_mail_address_moo
			Me.OFFICER_MAIL_ADDRESS_BUILDING.Text = .officer_mail_address_building
			Me.OFFICER_MAIL_ADDRESS_SOI.Text = .officer_mail_address_soi
			Me.OFFICER_MAIL_ADDRESS_STREET.Text = .officer_mail_address_street

			Common.Utility.SelectChoiceByText(OFFICER_MAIL_ADDRESS_PROVINCE, .officer_mail_address_province)
			Me.LoadDistinct2()
			Common.Utility.SelectChoiceByText(OFFICER_MAIL_ADDRESS_DISTRICT, .officer_mail_address_district)
			Me.LoadSubDistinct2()
			Common.Utility.SelectChoiceByText(OFFICER_MAIL_ADDRESS_SUB_DISTRICT, .officer_mail_address_sub)
			Me.LoadPostCode2()
			Common.Utility.SelectChoiceByText(OFFICER_MAIL_ADDRESS_POSTCODE, .officer_mail_address_postcode)

			Me.OFFICER_MAIL_PHONE.Text = .officer_mail_phone
			Me.OFFICER_MAIL_EMAIL.Text = .officer_mail_email

		End With
	End Sub

	Protected Sub btnCancel_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnCancel.Click
		Me.Response.Redirect("mainpage.aspx", False)
		RaiseEvent Cancel()
	End Sub

	Protected Sub OFFICER_CARD_ADDRESS_PROVINCE_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles OFFICER_CARD_ADDRESS_PROVINCE.SelectedIndexChanged
		Me.LoadDistinct1()
		Me.LoadSubDistinct1()
		Me.LoadPostCode1()
	End Sub

	Protected Sub OFFICER_CARD_ADDRESS_DISTRICT_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles OFFICER_CARD_ADDRESS_DISTRICT.SelectedIndexChanged
		Me.LoadSubDistinct1()
		Me.LoadPostCode1()
	End Sub

	Protected Sub officer_card_address_subICT_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles officer_card_address_subICT.SelectedIndexChanged
		Me.LoadPostCode1()
	End Sub

	Protected Sub OFFICER_MAIL_ADDRESS_PROVINCE_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles OFFICER_MAIL_ADDRESS_PROVINCE.SelectedIndexChanged
		Me.LoadDistinct2()
		Me.LoadSubDistinct2()
		Me.LoadPostCode2()
	End Sub

	Protected Sub OFFICER_MAIL_ADDRESS_DISTRICT_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles OFFICER_MAIL_ADDRESS_DISTRICT.SelectedIndexChanged
		Me.LoadSubDistinct2()
		Me.LoadPostCode2()
	End Sub

	Protected Sub OFFICER_MAIL_ADDRESS_SUB_DISTRICT_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles OFFICER_MAIL_ADDRESS_SUB_DISTRICT.SelectedIndexChanged
		Me.LoadPostCode2()
	End Sub

	Protected Sub btnCopy_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnCopy.Click
		Me.OFFICER_MAIL_ADDRESS_NO.Text = Me.OFFICER_CARD_ADDRESS_NO.Text
		Me.OFFICER_MAIL_ADDRESS_BUILDING.Text = Me.OFFICER_CARD_ADDRESS_BUILDING.Text
		Me.OFFICER_MAIL_ADDRESS_MOO.Text = Me.OFFICER_CARD_ADDRESS_MOO.Text
		Me.OFFICER_MAIL_ADDRESS_BUILDING.Text = Me.OFFICER_CARD_ADDRESS_BUILDING.Text
		Me.OFFICER_MAIL_ADDRESS_SOI.Text = Me.OFFICER_CARD_ADDRESS_SOI.Text
		Me.OFFICER_MAIL_ADDRESS_STREET.Text = Me.OFFICER_CARD_ADDRESS_STREET.Text
		Common.Utility.SelectChoice(OFFICER_MAIL_ADDRESS_PROVINCE, OFFICER_CARD_ADDRESS_PROVINCE.SelectedValue)
		Me.LoadDistinct2()
		Common.Utility.SelectChoice(OFFICER_MAIL_ADDRESS_DISTRICT, OFFICER_CARD_ADDRESS_DISTRICT.SelectedValue)
		Me.LoadSubDistinct2()
		Common.Utility.SelectChoice(OFFICER_MAIL_ADDRESS_SUB_DISTRICT, officer_card_address_subICT.SelectedValue)
		Me.LoadPostCode2()
		Common.Utility.SelectChoice(OFFICER_MAIL_ADDRESS_POSTCODE, OFFICER_CARD_ADDRESS_POSTCODE.SelectedValue)
	End Sub
End Class